﻿using AutoMapper;
using MediatR;
using Traceability.Doamin;
using Traceability.ErrorCode;
using Traceability.Infratruvture.Interfares;
using Traceability.Write.Api.Application.Command;

namespace Traceability.Write.Api.Application.CommandHander
{
    public class DelImageHander : IRequestHandler<DelImageCommand, ApiResult<int>>
    {
        private readonly IMapper mapper;
        private readonly IBaseRequsitory<Imager> baseRequsitory;

        public DelImageHander(IMapper mapper, IBaseRequsitory<Imager> baseRequsitory)
        {
            this.mapper = mapper;
            this.baseRequsitory = baseRequsitory;
        }

        /// <summary>
        /// 删除产品检验图片
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<int>> Handle(DelImageCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();

            var list = baseRequsitory.GetAll().Where(x => x.ImagerName == request.ImagerName).FirstOrDefault();

            baseRequsitory.Del(list);

            result.Code = ApiEnum.删除成功;
            result.Msg = ApiEnum.删除成功.ToString();

            return Task.FromResult(result);
        }
    }
}
